package cn.buaa.anniversary.controller;

import cn.buaa.anniversary.model.QueryForm;
import cn.buaa.anniversary.model.Word;
import cn.buaa.anniversary.msg.FileMessage;
import cn.buaa.anniversary.service.ImgService;
import cn.buaa.anniversary.service.PathService;
import cn.buaa.anniversary.service.QueryService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
public class GreetController {

    @Value("${buaa.static.greet.qr.path}")
    public String QRPath;

    @Value("${buaa.static.greet.template.dir}")
    public String templateDir;

    /*
     *  依赖注入
     */
    @Autowired
    public GreetController(QueryService queryService, ImgService imgService, PathService pathService) {
        this.queryService = queryService;
        this.imgService = imgService;
        this.pathService = pathService;
    }

    private static int anInt = 0;

    @PostMapping("/greet")
    public FileMessage greet(@Valid @RequestBody QueryForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new RuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }
        //获取底板
        String templatePath = queryService.findOne(form.getQuery());
        if (null == templatePath) {
            return new FileMessage("error", "没有匹配");
        }
        //读 底板 和 二维码
        File qrFile = new File(QRPath);
        File templateFile = new File(templateDir + "/" + templatePath);
        if (!templateFile.exists() || !qrFile.exists()) {
            System.out.println(templateFile.getAbsolutePath()+" "+templateFile.exists());
            System.out.println(qrFile.getAbsolutePath()+" "+qrFile.exists());
            return new FileMessage("error", "缺少素材文件");
        }
        File result;
        try {
            //获取合成结果
            result = imgService.createGreetImgWithName(form.getName(), templateFile, qrFile);
            //转换成url
            String url = pathService.convertImgResultToHttpUrl(result);
            //生成结果返回
            anInt++;
            log.debug("No: " + anInt + " -- " + form.getName());
            return new FileMessage("0", "ok", url, result.getName());
        } catch (IOException e) {
            e.printStackTrace();
            return new FileMessage("error", "文件读写异常");
        }


    }

    @ExceptionHandler(Exception.class)
    public FileMessage handleException(Exception e) {
        return new FileMessage("error", e.getMessage());
    }

    private final
    QueryService queryService;

    private final
    ImgService imgService;

    private final
    PathService pathService;
}
